package com.ruoyi.project.appConnector;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.framework.page.BaseQuery;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.configureSystem.domain.StationPicture;
import com.ruoyi.project.configureSystem.domain.SysArea;
import com.ruoyi.project.configureSystem.domain.VStation;
import com.ruoyi.project.configureSystem.service.IStationPictureService;
import com.ruoyi.project.dataviewSystem.domain.VWarning;
import com.ruoyi.project.dataviewSystem.domain.VWarningPicture;
import com.ruoyi.project.dataviewSystem.service.IVWarningPictureService;
import com.ruoyi.project.dataviewSystem.service.IVWarningService;
import com.ruoyi.project.orderSystem.domain.VDeclare;
import com.ruoyi.project.orderSystem.domain.VDeclareImage;
import com.ruoyi.project.orderSystem.service.IVDeclareImageService;
import com.ruoyi.project.orderSystem.service.IVDeclareService;
import com.ruoyi.project.query.StationRecordQuery;
import com.ruoyi.project.query.VDeclareQuery;
import com.ruoyi.project.query.WaiqingRecordQuery;
import com.ruoyi.project.reportSystem.domain.AttendanceRecord;
import com.ruoyi.project.reportSystem.domain.AttendanceRecordImage;
import com.ruoyi.project.reportSystem.domain.WaiqingRecord;
import com.ruoyi.project.reportSystem.domain.WaiqingRecordImage;
import com.ruoyi.project.reportSystem.service.IAttendanceRecordImageService;
import com.ruoyi.project.reportSystem.service.IAttendanceRecordService;
import com.ruoyi.project.reportSystem.service.IWaiqingRecordImageService;
import com.ruoyi.project.reportSystem.service.IWaiqingRecordService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by huaxin15355 on 2020/5/17.
 * APP接口
 */
@RestController
@RequestMapping("/app/data")
@CrossOrigin
public class AppController extends BaseController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private IAttendanceRecordService attendanceRecordService;
    @Autowired
    private IAttendanceRecordImageService iAttendanceRecordImageService;
    @Autowired
    private IVDeclareService ivDeclareService;
    @Autowired
    private IVDeclareImageService ivDeclareImageService;
    @Autowired
    private IVWarningService ivWarningService;

    @Autowired
    private IStationPictureService iStationPictureService;

    @Autowired
    private IVWarningPictureService ivWarningPictureService;

    @Autowired
    private IWaiqingRecordService iWaiqingRecordService;

    @Autowired
    private IWaiqingRecordImageService iWaiqingRecordImageService;



    /**
     * app测试
     */
    @GetMapping("/list")
    @CrossOrigin
    public AjaxResult list(SysArea area,HttpServletResponse response)
    {
        //添加跨域CORS
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type,token");
        response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
        AjaxResult ajaxResult = AjaxResult.success();
        List<SysArea> Areas = new ArrayList<>();
        area.setAreaName("222");
        Areas.add(area);
        ajaxResult.put("area",area);
        return ajaxResult;
    }

    /**
     * app端根据用户名查询用户的信息
     */
    @GetMapping("/selectUserInfoListByusername")
    @CrossOrigin
    public AjaxResult selectUserInfoListByusername(String userName)
    {
        AjaxResult ajaxResult = AjaxResult.success();
        SysUser  applogUser=userService.selectUserByUserName(userName);
        String av=applogUser.getAvatar();
        applogUser.setAvatar(serverConfig.getUrl()+av);
        ajaxResult.put("applogUser",applogUser);
        return ajaxResult;
    }


    /**
     * app端修改用户的信息
     */
    @GetMapping("/updateAppuserinfo")
    @CrossOrigin
    public AjaxResult updateAppuserinfo(SysUser applogUser)
    {
        AjaxResult ajaxResult = AjaxResult.success();
        int i=userService.updateUserinfoByusername(applogUser);
        return ajaxResult;
    }

    /**
     * app个人资料头像上传
     */
    @Log(title = "用户头像", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    @CrossOrigin
    public AjaxResult avatar(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws IOException
    {
        if (!file.isEmpty())
        {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
            if (userService.updateUserAvatar(loginUser.getUsername(), avatar))
            {
                AjaxResult ajax = AjaxResult.success();
                String url = serverConfig.getUrl() + avatar;
                ajax.put("imgUrl", url);
                // 更新缓存用户头像
                loginUser.getUser().setAvatar(url);
                tokenService.setLoginUser(loginUser);
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }



    /**
     * app端根据用户名修改用户的密码
     */
    @GetMapping("/updatePasswordByusername")
    @CrossOrigin
    public AjaxResult updatePasswordByusername(String userName,String password)
    {
        AjaxResult ajaxResult = AjaxResult.success();
        SysUser user=new SysUser();
        user.setUserName(userName);
        user.setPassword(SecurityUtils.encryptPassword(password));
        int i=userService.updateUserinfoByusername(user);
        if (i>0){
            return ajaxResult;
        }else {
            return ajaxResult.error("修改失败，请联系管理员");
        }
    }

    /**
     * app端根据用户名查询当天的用户的打卡记录
     */
    @GetMapping("/selecAttendanceRecordByusername")
    @CrossOrigin
    public AjaxResult selecAttendanceRecordByusername(String userName,String stationcode,
      String stationName,String signinTime,String signoutTime) throws ParseException {
        AjaxResult ajaxResult = AjaxResult.success();
        AttendanceRecord attendanceRecord=new AttendanceRecord();
        attendanceRecord.setChargeUser(userName);
        attendanceRecord.setStationcode(stationcode);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());//这个就是把时间戳经过处理得到期望格式的时间
        if (signinTime!=""){
            attendanceRecord.setSigninTime(signinTime);
        }else{
            attendanceRecord.setSigninTime(time);
        }
        if (signoutTime!=""){
            attendanceRecord.setSigninTime(signoutTime);
        }
        attendanceRecord=attendanceRecordService.selecAttendanceRecordByusername(attendanceRecord);

//        attendanceRecord.setSigninTime(sign);
//        attendanceRecord.setSignoutTime(out);
        ajaxResult.put("attendanceRecord",attendanceRecord);
        return ajaxResult;
    }


    /**
     * app端根据用户进行签到操作
     */
    @GetMapping("/addqiandaoRecord")
    @CrossOrigin
    public AjaxResult addqiandaoRecord(AttendanceRecord attendanceRecord){
        AjaxResult ajaxResult = AjaxResult.success();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (attendanceRecord.getSigninTime()!=""){
            attendanceRecord.setUserId(loginUser.getUser().getUserId());
            int i=attendanceRecordService.addqiandaoRecord(attendanceRecord);
            ajaxResult.put("attendanceRecord",attendanceRecord);
            return ajaxResult;
        }
        return AjaxResult.error("签到失败");
    }
    /**
     * app端根据用户进行更新签到和签退操作
     */
    @GetMapping("/updataqiandaoRecord")
    @CrossOrigin
    public AjaxResult updataqiandaoRecord(AttendanceRecord attendanceRecord){
        AjaxResult ajaxResult = AjaxResult.success();
        int i=attendanceRecordService.updateAttendanceRecord(attendanceRecord);
        ajaxResult.put("attendanceRecord",attendanceRecord);

        return ajaxResult;
    }

    /**
     * app用户上报考勤图片
     */
    @Log(title = "app用户上报考勤图片", businessType = BusinessType.UPDATE)
    @PostMapping("/addpictureattendance")
    @CrossOrigin
    public AjaxResult addpictureattendance(MultipartRequest request,int totalnum,long id,
    String description,String username,String stationCode,String stationName) throws IOException
    {
        AjaxResult ajaxResult = AjaxResult.success();
        Map<String,Object> map = new HashMap<String,Object>();
        // 这样就可以收到文件了，files.length == 1.
        List<MultipartFile> files=new ArrayList<MultipartFile>();
        for (int i=0;i<totalnum;i++){
            System.err.println(request.getFile("image"+i)+"=============================");
            files.add(request.getFile("image"+i));
        }
        //先保存描述信息  站点code  用户名  当前时间  三个条件保证当天某个人某个站点记录唯一
        AttendanceRecord attendanceRecord=new AttendanceRecord();
        attendanceRecord.setId(id);
        attendanceRecord.setDescription(description);
        int at=attendanceRecordService.updateAttendanceRecord(attendanceRecord);
        //保存图片
        for (int i=0;i<files.size();i++){
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, files.get(i));
            String url = serverConfig.getUrl() + fileName;
            //保存文件名称和路径放在数据库  一次打卡提交可以有多张图片
            AttendanceRecordImage attendanceRecordImage=new AttendanceRecordImage();
            attendanceRecordImage.setStationcode(stationCode);
            attendanceRecordImage.setImageName(fileName);
            attendanceRecordImage.setImageUrl(url);
            attendanceRecordImage.setAttencordId(id);
            iAttendanceRecordImageService.insertAttendanceRecordImage(attendanceRecordImage);
        }
        ajaxResult.put("id",attendanceRecord.getId());
        return ajaxResult;
    }


    /**
     * app用户外勤打卡传图片和记录
     */
    @Log(title = "app用户外勤打卡传图片和记录", businessType = BusinessType.UPDATE)
    @PostMapping("/addpictureWaiqingcord")
    @CrossOrigin
    public AjaxResult addpictureWaiqingcord(MultipartRequest request,int totalnum,
                                           String aaddress,String beizhu) throws IOException
    {
        AjaxResult ajaxResult = AjaxResult.success();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(new Date());//这个就是把时间戳经过处理得到期望格式的时间
        Map<String,Object> map = new HashMap<String,Object>();
        // 这样就可以收到文件了，files.length == 1.
        List<MultipartFile> files=new ArrayList<MultipartFile>();
        for (int i=0;i<totalnum;i++){
            System.err.println(request.getFile("image"+i)+"=============================");
            files.add(request.getFile("image"+i));
        }
        //每提交一次，就保存一次记录到外勤打卡表中
        WaiqingRecord waiqingRecord=new WaiqingRecord();
        waiqingRecord.setQiandaoTime(time);
        waiqingRecord.setAaddress(aaddress);
        waiqingRecord.setBeizhu(beizhu);
        waiqingRecord.setChargeUsername(loginUser.getUser().getUserName());
        waiqingRecord.setUserid(loginUser.getUser().getUserId());
        int at=iWaiqingRecordService.insertWaiqingRecord(waiqingRecord);
        //保存图片
        for (int i=0;i<files.size();i++){
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, files.get(i));
            String url = serverConfig.getUrl() + fileName;
            //保存文件名称和路径放在数据库  一次打卡提交可以有多张图片
            WaiqingRecordImage waiqingRecordImage=new WaiqingRecordImage();
            waiqingRecordImage.setImageName(fileName);
            waiqingRecordImage.setImageUrl(url);
            waiqingRecordImage.setWaiqingId(waiqingRecord.getId());
            iWaiqingRecordImageService.insertWaiqingRecordImage(waiqingRecordImage);
        }
        return ajaxResult;
    }


    /**
     * app 查询用户外勤打卡的记录（分页）
     */
    @PostMapping("/appUserWaiqingListRecord")
    @CrossOrigin
    public AjaxResult appUserWaiqingListRecord(@RequestBody WaiqingRecordQuery waiqingRecordQuery)
    {
        PageHelper.startPage(waiqingRecordQuery.getPageNum(), waiqingRecordQuery.getPageSize());
        String date=waiqingRecordQuery.getDate();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());//这个就是把时间戳经过处理得到期望格式的时间

        WaiqingRecord waiqingRecord=new WaiqingRecord();
        if (date!="" && date!=null){
            waiqingRecord.setQiandaoTime(date);
        }else{
            waiqingRecord.setQiandaoTime(time);
        }
        waiqingRecord.setUserid(loginUser.getUser().getUserId());
        List<WaiqingRecord> waiqingRecordimageListList=iWaiqingRecordService.selectWaiqingRecordimageList(waiqingRecord);
        return AjaxResult.success(new PageInfo<>(waiqingRecordimageListList));
    }



    /**
     * app 查询用户申报记录列表（分页）
     */
    @PostMapping("/appUsergetdeclareList")
    @CrossOrigin
    public AjaxResult appUsergetdeclareList(@RequestBody VDeclareQuery vDeclareQuery)
    {
        PageHelper.startPage(vDeclareQuery.getPageNum(), vDeclareQuery.getPageSize());
        String date=vDeclareQuery.getDate();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());//这个就是把时间戳经过处理得到期望格式的时间

        VDeclare vdeclare=new VDeclare();
        if (date!="" && date!=null){
            vdeclare.setUploadTime(date);
        }else{
            vdeclare.setUploadTime(time);
        }
        vdeclare.setUserId(loginUser.getUser().getUserId());
        List<VDeclare> declareimageListList=ivDeclareService.selectdeclareimageList(vdeclare);
        return AjaxResult.success(new PageInfo<>(declareimageListList));
    }


    /**
     * app 查询用户站点打卡的记录（分页）
     */
    @PostMapping("/appUserstationListRecord")
    @CrossOrigin
    public AjaxResult appUserstationListRecord(@RequestBody StationRecordQuery stationRecordQuery)
    {
        PageHelper.startPage(stationRecordQuery.getPageNum(), stationRecordQuery.getPageSize());
        String date=stationRecordQuery.getDate();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());//这个就是把时间戳经过处理得到期望格式的时间

        AttendanceRecord attendanceRecord=new AttendanceRecord();
        if (date!="" && date!=null){
            attendanceRecord.setSigninTime(date);
        }else{
            attendanceRecord.setSigninTime(time);
        }
        attendanceRecord.setUserId(loginUser.getUser().getUserId());
        List<AttendanceRecord> stationRecordimageListList=attendanceRecordService.selectUserstationListRecord(attendanceRecord);
        return AjaxResult.success(new PageInfo<>(stationRecordimageListList));
    }



    /**
     * app站点列表上传图片
     */
    @Log(title = "app站点列表上传图片", businessType = BusinessType.UPDATE)
    @PostMapping("/addstationpicture")
    @CrossOrigin
    public AjaxResult addstationpicture(MultipartRequest request,int totalnum,long id) throws IOException
    {
        AjaxResult ajaxResult = AjaxResult.success();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Map<String,Object> map = new HashMap<String,Object>();
        // 这样就可以收到文件了，files.length == 1.
        List<MultipartFile> files=new ArrayList<MultipartFile>();
        for (int i=0;i<totalnum;i++){
            System.err.println(request.getFile("image"+i)+"=============================");
            files.add(request.getFile("image"+i));
        }
        //保存图片
        for (int i=0;i<files.size();i++){
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
//            String  fileName = FileUploadUtils.stationPicUpload(filePath, files.get(i));
            String fileName = FileUploadUtils.upload(filePath, files.get(i));
            System.out.print(fileName);
            String url = serverConfig.getUrl() + fileName;
            //保存文件名称和路径放在数据库  一次可提交可以有多张图片
            System.out.print(url);
            StationPicture stationPicture=new StationPicture();
            stationPicture.setStationId(id);
            stationPicture.setUrl(url);
            stationPicture.setUploadBy(loginUser.getUser().getUserId());
            stationPicture.setGmtUpload(new Date());
            iStationPictureService.insertStationPicture(stationPicture);
        }
        return ajaxResult;
    }

    /**
     * app端根据//根据add页面传过来的attendanceId来查询当前用户，当前天的打卡记录
     */
    @GetMapping("/selectTodayRecordList")
    @CrossOrigin
    public AjaxResult selectTodayRecordList(String userName,String date) throws ParseException {
        AjaxResult ajaxResult = AjaxResult.success();
        AttendanceRecord attendanceRecord=new AttendanceRecord();
        attendanceRecord.setChargeUser(userName);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());//这个就是把时间戳经过处理得到期望格式的时间
        if (date!=""){
            attendanceRecord.setSigninTime(date);
        }else{
            attendanceRecord.setSigninTime(time);
        }
        List<AttendanceRecord> attendanceRecordlist=attendanceRecordService.selectTodayRecordList(attendanceRecord);
        ajaxResult.put("attendanceRecordlist",attendanceRecordlist);
        return ajaxResult;
    }


    /**
     * app用户故障申报
     */
    @Log(title = "app用户故障申报", businessType = BusinessType.UPDATE)
    @PostMapping("/addpicturerepair")
    @CrossOrigin
    public AjaxResult addpicturerepair(MultipartRequest request,int totalnum,
                                           String content,String declaretype,String stationCode,String stationName) throws IOException
    {
        AjaxResult ajaxResult = AjaxResult.success();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        VDeclare vDeclare=new VDeclare();
        vDeclare.setStationcode(stationCode);
        vDeclare.setStationname(stationName);
        vDeclare.setChargeuser(user.getUserName());
        vDeclare.setUserId(user.getUserId());
        vDeclare.setProblemDeclare(declaretype);
        vDeclare.setErrorDescription(content);
        vDeclare.setFaultState("0");//未处理状态
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(new Date());//这个就是把时间戳经过处理得到期望格式的时间
        vDeclare.setUploadTime(time);
        int iv=ivDeclareService.insertVDeclare(vDeclare);
        if (iv>0){
            Map<String,Object> map = new HashMap<String,Object>();
            // 这样就可以收到文件了，files.length == 1.
            List<MultipartFile> files=new ArrayList<MultipartFile>();
            for (int i=0;i<totalnum;i++){
                System.err.println(request.getFile("image"+i)+"=============================");
                files.add(request.getFile("image"+i));
            }
            //保存图片
            for (int i=0;i<files.size();i++){
                String filePath = RuoYiConfig.getUploadPath();
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, files.get(i));
                String url = serverConfig.getUrl() + fileName;
                //保存文件名称和路径放在数据库  一次打卡提交可以有多张图片
                VDeclareImage vDeclareImage=new VDeclareImage();
                vDeclareImage.setStationcode(stationCode);
                vDeclareImage.setImageName(fileName);
                vDeclareImage.setImageUrl(url);
                vDeclareImage.setDeclareId(vDeclare.getId());
                ivDeclareImageService.insertVDeclareImage(vDeclareImage);
            }
        }

        return ajaxResult;
    }


    //告警

    /**
     * app端根据用户获取实时告警
     */
    @GetMapping("/getwarningdata")
    @CrossOrigin
    public TableDataInfo getwarningdata(String warnState)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        根据用户去查该用户能看到的站点，然后再根据站点code筛选告警列表中的告警数据
        String stationCode="LDKS784471";
        VWarning vWarning=new VWarning();
        vWarning.setStationcode(stationCode);
        if (warnState!=null && warnState!=""){
            vWarning.setWarnState(warnState);
        }
        List<VWarning> vWarningList=ivWarningService.selectVWarningList(vWarning);

        return getDataTable(vWarningList);
    }

    /**
     * app端根据用户获取历史告警和对应图片
     */
    @GetMapping("/getoldWarningList")
    @CrossOrigin
    public TableDataInfo getoldWarningList(String warnState)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        根据用户去查该用户能看到的站点，然后再根据站点code筛选告警列表中的告警数据
        String stationCode="LDKS784471";
        VWarning vWarning=new VWarning();
        vWarning.setStationcode(stationCode);
        if (warnState!=null && warnState!=""){
            vWarning.setWarnState(warnState);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());//这个就是把时间戳经过处理得到期望格式的时间
        vWarning.setFinishdate(time);
        List<VWarning> vWarningList=ivWarningService.getoldWarningList(vWarning);

        return getDataTable(vWarningList);
    }
    /**
     * app端根据//告警忽略操作处理
     */
    @Log(title = "app告警忽略操作处理", businessType = BusinessType.UPDATE)
    @GetMapping("/ignorewaring")
    @CrossOrigin
    public AjaxResult ignorewaring(VWarning vWarning){
        AjaxResult ajaxResult = AjaxResult.success();
        vWarning.setWarnState("2");
        int i=ivWarningService.updateVWarning(vWarning);
        return ajaxResult;
    }

    /**
     * app告警列表，处理生成的告警操作
     */
    @Log(title = "app告警列表，处理生成的告警操作", businessType = BusinessType.UPDATE)
    @PostMapping("/dealwaring")
    @CrossOrigin
    public AjaxResult dealwaring(MultipartRequest request,int totalnum,long id,String description) throws IOException
    {
        AjaxResult ajaxResult = AjaxResult.success();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());

        //更新告警的状态，由未处理状态变为已处理状态 即0变为1的状态  2是已忽略状态
        VWarning vWarning=new VWarning();
        vWarning.setId(id);
        vWarning.setWarnState("1");//已处理状态
        vWarning.setResolvent(description);//处理的告警描述
        int vw=ivWarningService.updateVWarning(vWarning);
        if (vw>0){
            //接收存储照片
            Map<String,Object> map = new HashMap<String,Object>();
            // 这样就可以收到文件了，files.length == 1.
            List<MultipartFile> files=new ArrayList<MultipartFile>();
            for (int i=0;i<totalnum;i++){
                System.err.println(request.getFile("image"+i)+"=============================");
                files.add(request.getFile("image"+i));
            }
            //保存图片
            for (int i=0;i<files.size();i++){
                String filePath = RuoYiConfig.getUploadPath();
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, files.get(i));
                String url = serverConfig.getUrl() + fileName;
                //保存文件名称和路径放在数据库  一次可提交可以有多张图片
                VWarningPicture vWarningPicture=new VWarningPicture();
                vWarningPicture.setImageUrl(url);
                vWarningPicture.setDescription(description);
                vWarningPicture.setVwaringId(id);//告警id作为外键id
                vWarningPicture.setDealTime(new Date());
                vWarningPicture.setDealUsername(loginUser.getUsername());
                ivWarningPictureService.insertVWarningPicture(vWarningPicture);

            }
        }


        return ajaxResult;
    }

    @GetMapping("/authToken")
    public AjaxResult authToken(){
        return AjaxResult.success();
    }

    /**
     * 通用上传请求
     */
    @PostMapping("/common/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }


}
